package com.ostrich.module_home.nominate

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ostrich.library_base.BaseMvvmFragment
import com.ostrich.library_common.utils.LoggerUtils
import com.ostrich.module_home.R
import com.ostrich.module_home.databinding.HomeFragmentNominateBinding
import com.ostrich.module_home.databinding.HomeItemBannerViewBinding
import com.ostrich.module_home.nominate.adapter.NominateAdapter
import com.ostrich.module_home.nominate.entity.NominateEntity
import com.ostrich.module_home.nominate.viewholder.CustomPagerViewHolder
import com.zhpan.bannerview.constants.PageStyle
import com.zhpan.bannerview.constants.TransformerStyle
import com.zhpan.bannerview.holder.HolderCreator


class NominateFragment : BaseMvvmFragment<HomeFragmentNominateBinding,NominateViewModel>(),
    HolderCreator<CustomPagerViewHolder> {
    private lateinit var  nominateAdapter: NominateAdapter
    override fun initView() {
        nominateAdapter = NominateAdapter()
        binding?.let {
            it.rv.adapter = nominateAdapter
            it.rv.layoutManager = LinearLayoutManager(context)
            //recyclerview优化：item固定，每次复用不需要重新测量
            it.rv.setHasFixedSize(true)
            //添加头布局
            addHeadView(it.rv)
        }

    }
    private fun addHeadView(rv:RecyclerView){
        val headBinding =
            DataBindingUtil.inflate<HomeItemBannerViewBinding>(LayoutInflater.from(context),
                R.layout.home_item_banner_view,
                rv,
                false)
        val list = mutableListOf<String>()
        list.add("http://ali-img.kaiyanapp.com/5936fce2dbf262e4b977b2d37950b3f0.jpeg?imageMogr2/quality/60/format/jpg")
        list.add("http://ali-img.kaiyanapp.com/5936fce2dbf262e4b977b2d37950b3f0.jpeg?imageMogr2/quality/60/format/jpg")
        list.add("http://ali-img.kaiyanapp.com/5936fce2dbf262e4b977b2d37950b3f0.jpeg?imageMogr2/quality/60/format/jpg")
        headBinding.bvTop
            .setCanLoop(true)
            .setPageStyle(PageStyle.MULTI_PAGE_OVERLAP)//多页展示
            .setHolderCreator(this)
            .create(list)
        nominateAdapter.addHeaderView(headBinding.root)
    }


    override fun initData() {
        viewModel.nominate().observe(this){ it ->
            LoggerUtils.d(TAG,it.toString())
            //解析转换数据
            val newList = mutableListOf<NominateEntity>()//新数据源集合
            val itemList = it.itemList //网络请求数据源
            itemList.forEach {
                newList.add(it)
                if("squareCardCollection" == it.type){
                    val itemList1 = it.data.itemList
                    newList.addAll(itemList1)
                }
            }
            //填充数据
            nominateAdapter.setList(newList)
        }

    }

    override fun initListener() {

    }

    override fun bindLayout(): Int {
        return R.layout.home_fragment_nominate
    }

    override fun getJVMClass(): Class<NominateViewModel> {
       return NominateViewModel::class.java
    }

    companion object{
        const val TAG = "NominateFragment"
    }

    override fun createViewHolder(): CustomPagerViewHolder {
        return CustomPagerViewHolder()
    }


}